package com.wjcxf.paint;


import com.wjcxf.entry.DeviceEntry;
import com.wjcxf.struct.PopStar;
import com.wjcxf.utils.AdbUtil;
import com.wjcxf.utils.ImageUtil;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;

/**
 * @author 我叫程晓非
 * @desc PaintGrid绘图类
 * @date 2022-03-15
 * @version 1.0
 */
public class PaintGrid {
    int x;
    int y;
    int height;
    int width;
    int row;
    int col;
    float scale = 1.0f;
    int imgX;
    int imgY;
    //设置取样点偏移
    float offsetX = 0.23f;
    float offsetY = 0.23f;
    private java.util.ArrayList<Point> tipsList = new ArrayList<>();

    int [][]starArr = new int[PopStar.ROW][PopStar.COL];
    Color color;
    BufferedImage bufferedImage;

    private boolean isShowTips = false;
    private int tipsX;
    private int tipsY;
    private int tipsWidth;
    private int tipsHeight;

    public static final int MAX_WIDTH = 600;
    public static final int MAX_HEIGHT = 600;
    public static final int MIN_WIDTH = 100;
    public static final int MIN_HEIGHT = 100;
    public static BufferedImage tipsImage;
    private PopStar star = null;
    private DeviceEntry<String,String> deviceEntry = null;
    public int currentIndex = 0;
    static {
        try {
            tipsImage = ImageIO.read(new File("./src/image/tips.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public PaintGrid( int x, int y, int height, int width, Color color) {
        this.x = x;
        this.y = y;
        this.height = height;
        this.width = width;
        this.color = color;
        this.row = PopStar.ROW;
        this.col = PopStar.COL;
    }




    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getCol() {
        return col;
    }

    public void setCol(int col) {
        this.col = col;
    }

    public void setX(int x,int imgX,int imgWidth) {
        if(x >= imgX && x <= imgX+imgWidth - this.width){
            this.x = x;
        }
    }
    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y,int imgY,int imgHeight) {
        if(y >= imgY && y <= imgY+imgHeight -this.height){
            this.y = y;
        }
    }
    public void setY(int y) {
        this.y = y;
    }

    public void setHeight(int height) {
        if(height >= MIN_HEIGHT && height<=MAX_HEIGHT){
            this.height = height;
        }
    }

    public void setWidth(int width) {
        if(width >= MIN_WIDTH && width<=MAX_WIDTH){
            this.width = width;
        }
    }

    public int getHeight() {
        return height;
    }

    public int getWidth() {
        return width;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public void draw(Graphics g){

        g.setColor(color);

        int h = height/row;
        int w = width/col;
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                g.setColor(color);
                g.drawRect(x+i*w,y+j*h,w,h);
                if(bufferedImage != null){
                    //1.获取手机相对坐标
                    Point mobilePosition = getMobilePosition(i,j);
                    //2.从BufferImage获取颜色
//                    System.out.println(mobilePosition.x+":"+ mobilePosition.y);
                    int offsetScaleX = (int)((w*offsetX)*scale);
                    int offsetScaleY = (int)((h*offsetY)*scale);
                    //方案一：范围采样+统计投票
                    //方案二：范围采样+平均值
                    //方案三：聚类算法
//                    int typeArr[] = new int[6];
//                    Arrays.fill(typeArr,0);
                    Color color = new Color(this.bufferedImage.getRGB(mobilePosition.x+offsetScaleX,mobilePosition.y+offsetScaleY));
//                    for(int k = mobilePosition.x;k<mobilePosition.x+2*offsetScaleX;k++){
//                        for(int m = mobilePosition.y;m< mobilePosition.y +2*offsetScaleY;m++){
//                            if(k < this.bufferedImage.getWidth() && m < this.bufferedImage.getHeight()){
//                                Color c = new Color(this.bufferedImage.getRGB(k,m));
//                                int type = ImageUtil.identifyColor(c,30);
//                                typeArr[type] ++;
//                            }
//
//                        }
//                    }
//                    typeArr[0] = 0;
//                    System.out.println("sumRed:"+sumR);
//                    System.out.println("total:"+total);
//                    int mobileY = (int)((y+j*h+(int)(h*offsetY))*1);
//                    int mobileX = (int)((x+i*w+(int)(w*offsetX))*1);
//                    Color color = new Color(this.bufferedImage.getRGB(mobileX,mobileY));
                    //
                    int type = ImageUtil.identifyColor(color,35);
                    starArr[j][i] = type;
                    Color identifyColor = ImageUtil.getColorByType(type);
                    g.setColor(identifyColor);
                    g.fillRect(x+i*w + w/2-(int)(w*offsetX)/2,y+j*h + h/2 - (int)(h*offsetY)/2,(int)(w*offsetX),(int)(h*offsetY));
                }

            }
        }
        if(isShowTips){
            g.drawImage(tipsImage,tipsX,tipsY,tipsWidth,tipsHeight,null);
        }
    }
    //获取最大值的下标
    private int getMaxIndex(int[] arr) {
        int max = arr[0];
        int  index = 0;
        for (int i = 0; i < arr.length; i++) {
            if (max < arr[i]) {
                max = arr[i];
                index = i;
            }
        }
        return index;
    }
    public Point getMobilePosition(int i,int j){
        Point result = new Point();
        int h = height/row;
        int w = width/col;
        int rowGridX = x + i*w - imgX;
        int colGridY = y + j*h - imgY;

        int offsetScaleX = (int)((w*offsetX)*scale);
        int offsetScaleY = (int)((h*offsetY)*scale);
        result.x = (int)(scale*rowGridX) +offsetScaleX;
        result.y = (int)(scale*colGridY) +offsetScaleY;
        return result;
    }



//    public Point getMobilePosition2(int index,Rectangle rectangle,int imageX,int imageY){
//        Point result = new Point();
//        int h = height/row;
//        int w = width/col;
//        result.x = rectangle.x + x + (index%(row+1))*w - imageX;
//        result.y = rectangle.y + y + (index/(col+1))*h - imageY;
//        return result;
//    }

    public void setBufferedImage(BufferedImage bufferedImage) {
        this.bufferedImage = bufferedImage;
    }
    public int[][] getStarArr(){
        return this.starArr;
    }

    public float getScale() {
        return scale;
    }

    public void setScale(float scale) {
        this.scale = scale;
    }

    public int getImgX() {
        return imgX;
    }

    public void setImgX(int imgX) {
        this.imgX = imgX;
    }

    public int getImgY() {
        return imgY;
    }

    public void setImgY(int imgY) {
        this.imgY = imgY;
    }

    public boolean isShowTips() {
        return isShowTips;
    }

    public void setShowTips(boolean showTips,int i,int j) {

        isShowTips = showTips;
//        Point point = getMobilePosition(x,y);
        if(i == -100 && j == -100){
            return;
        }
        int h = height/row;
        int w = width/col;
        tipsX = j*h +this.y;
        tipsY = i*w +this.x;
        tipsHeight = height/row;
        tipsWidth = width/col;
    }

    public Rectangle getTipsRect() {
        return new Rectangle(tipsX,tipsY,tipsWidth,tipsHeight);
    }
    public void setTipsList(java.util.List<com.wjcxf.struct.Point> list,PopStar star){
        currentIndex = 0;
        this.tipsList.clear();
        for(int i=0;i<list.size();i++){
            tipsList.add(new Point(list.get(i).getX(),list.get(i).getY()));
        }
        this.star = star;
//        Point p = tipsList.get(currentIndex);
//        this.setShowTips(true,-100,-100);
        nextTips();
    }

    public void nextTips() {
        if(deviceEntry == null){
            return;
        }
        if(tipsList.size() ==0){
            this.setShowTips(false,0,0);
            return;
        }
        if(tipsList.size() == currentIndex){
            this.setShowTips(false,0,0);
            tipsList.clear();
            return;
        }


        Point p = tipsList.get(currentIndex);
        this.setShowTips(true,p.x,p.y);
        Point mobilePosition = getMobilePosition2(p.x,p.y);
        System.out.println("p.x = "+p.x+",p.y = "+p.y);
        String cmd = "adb -s "+this.deviceEntry.getKey().toString()+" shell input tap "+mobilePosition.x+" "+mobilePosition.y;
        if(star != null){
            star.click(p.x,p.y);
            star.checkGameOver();
            star.show();

        }
        currentIndex++;
        AdbUtil.executeShellPlus(cmd);
//        System.out.println(cmd);

        AdbUtil.executeShellPlus("adb -s" +this.deviceEntry.getKey().toString()+ " shell screencap -p /sdcard/screen.png");
        AdbUtil.executeShellPlus("adb pull /sdcard/screen.png ./sreenshot/"+this.deviceEntry.getKey().toString()+".png");

    }

    public Point getMobilePosition2(int i, int j) {
        Point result = new Point();
        int h = height/row;
        int w = width/col;
        int rowGridX = x + j*w - imgX;
        int colGridY = y + i*h - imgY;

        int offsetScaleX = (int)((w*offsetX)*scale);
        int offsetScaleY = (int)((h*offsetY)*scale);
        result.x = (int)(scale*rowGridX) +offsetScaleX;
        result.y = (int)(scale*colGridY) +offsetScaleY;
        return result;
    }

    public DeviceEntry<String, String> getDeviceEntry() {
        return deviceEntry;
    }

    public void setDeviceEntry(DeviceEntry<String, String> deviceEntry) {
        this.deviceEntry = deviceEntry;
    }
}
